package com.wontube.smr.web.controller.admin;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.web.client.admin.IAdminGenreHandler;

@Controller
@RequestMapping("/admin/genre")
public class AdminGenreController {
	private final static String SUCCESS = "Done";
	private final static String FAILURE = "Failure: something wrong";
	
	@RequestMapping("")
	public ModelAndView gotoGenreAdminHome(){
		ModelAndView mv = new ModelAndView("admin/genre/genre_index");
		//IAdminGenreHandler handler = getAdminGenreHandler();
		String[] categorys = Genre.getAllGenreCategories();
		//List<Genre> genres = handler.selectAllGenre();
		mv.addObject("categorys", categorys);
		//mv.addObject("genres", genres);
		return mv;
	}
	
	@RequestMapping("/list")
	public ModelAndView getGenreByCategory(@RequestParam("category") String category){
		ModelAndView mv = new ModelAndView("admin/genre/genre_list");
		IAdminGenreHandler handler = getAdminGenreHandler();
		List<Genre> genres = null;
		if("All".equals(category)){
			genres = handler.selectAllGenre();
		}else{
			genres = handler.selectGenreByCategory(category);
		}
		mv.addObject("genres", genres);
		return mv;
	}
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public String addGenre(Genre genre){
		String genreName = genre.getGenreName();
		if(genreName == null || "".equals(genreName.trim())){
			return "Genre Name cannot be empty";
		}
		
		if(genre.getCategory() == null || "".equals(genre.getCategory().trim())){
			return "Genre Category cannot be empty";
		}
		
		String dn = genre.getDisplayName();
		String un = genre.getUnitName();
		if(dn == null || "".equals(dn.trim())){
			genre.setDisplayName(genreName);
		}
		
		if(un == null || "".equals(un.trim())){
			genre.setUnitName(genreName);
		}		
	
		IAdminGenreHandler handler = getAdminGenreHandler();
		genre.setId(UUID.randomUUID().toString());
		genre.setVersion(new Date());
		boolean bl = handler.addGenre(genre);
		if(bl){
			return SUCCESS;
		}
		return "出错, 查看genre分类下是否有相同的genre了.";
	}
	
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public String deleteGenre(@PathVariable("id") String id){
		IAdminGenreHandler handler = getAdminGenreHandler();
		boolean bl = handler.deleteGenreByID(id);
		if(bl){
			return SUCCESS;
		}
		return FAILURE;
	}
	
	@RequestMapping("/get/{id}")
	public ModelAndView getGenreByID(@PathVariable("id") String id){
		ModelAndView mv = new ModelAndView("admin/genre/update_genre");
		IAdminGenreHandler handler = getAdminGenreHandler();
		String[] categorys = Genre.getAllGenreCategories();
		Genre genre = handler.getGenreByID(id);
		mv.addObject("categorys", categorys);	
		mv.addObject("genre", genre);
		return mv;
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public String updateGenre(Genre genre){
		genre.setVersion(new Date());
		IAdminGenreHandler handler = getAdminGenreHandler();
		boolean success = handler.updateGenre(genre);
		if(success){
			return SUCCESS;
		}
		return FAILURE;
	}
	
	private IAdminGenreHandler getAdminGenreHandler(){
		Remoting<IAdminGenreHandler> remoting = new Remoting<IAdminGenreHandler>();
		return remoting.createService(IAdminGenreHandler.class);
	}
}
